let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})

const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        Store_Name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        Opened_Date: [{ required: true, message: '请选择商品品牌', trigger: 'blur' }],
        Closed_Date: [{ required: true, message: '请选择商品类别', trigger: 'blur' }],
        Store_Type: [{ required: true, message: '请选择门店类别', trigger: 'change' }],
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      // 整个省市县数据
      city,
      value:[],//地址
      enterprise_appid:[],
      address:'',
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        Store_SerialNumber: '',
        Store_Name: '',
        Opened_Date: '',
        Closed_Date: '',
        Store_Type: '',
        Store_Address: '',
      },
      loading: false
    }
  },
  mounted() {
    const Store_SerialNumber = sessionStorage.getItem("Store_SerialNumber")
    this.getData(Store_SerialNumber)
  },
  methods: {
    getData(Store_SerialNumber) {
      const _this = this
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token='+ $.cookie('token'),
        type: 'POST',
        data: { 
          Data:'store_info',
          Primary_Key_Name:'Store_SerialNumber',
          Primary_Key:Store_SerialNumber
        },
        success: function (res) {
          _this.form = res[0]
          _this.value = res[0].Store_Address.split('/').slice(0, 3)
          _this.address = res[0].Store_Address.split('/')[3]
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
      $.ajax({
        url: `${DomainName}/getWeChatProgram?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Enterprise_Info_SerialNumber: $.cookie('enterprise'),
        },
        success: function (res) {
          if (res.Status == 'success') {
            _this.enterprise_appid = res.Data
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    handleChange(e){
      this.form.Store_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}/${e}`
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Data: JSON.stringify({ Modify: { store_info: [modify] } }) }
          } else {
            data = {
              Data: JSON.stringify(
                {
                  Modify: { store_info: [modify] },
                  Add: { store_info: [add] }
                }
              )
            }
          }
          _this.loading = true
          $.ajax({
            url: DomainName + '/GoodsFileEdit',
            type: 'POST',
            data,
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.history.go(-1);
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form)
      return { add: null, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.selectProvince = {}
      this.selectCity = {}
      this.selectArea = {}
      this.cityList = []
      this.areaList = []
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      console.log(event)
      this.form.Store_Address = this.selectProvince.label + "/" + this.selectCity.label + "/" + this.selectArea.label
    }
  }
})